#include "allhead.h"
#include "Graf.h"
#include "SurfCompress.h"

#define printf0 if(0)printf
#define printf1 if(0)printf

int SurfGrafPut(LPGRAF graf, LPFLOAT *p_points, int *p_np,LPINT *p_trg, int *p_nt)
{
 int     i;
 LPFLOAT points;
 int	 np;
 int	 nt; 

 np=graf->n;
 nt=graf->nt;

 if(!(*p_np))
	{
	points=alloc_float_mas_n1(3*np);

	*p_points=points;
	*p_trg=graf->trg;
	graf->trg=NULL;

	*p_np=np;
	*p_nt=nt; 

	for(i=0;i<np;i++)
		{
		points[     i]=graf->vertex[i].x;
		points[  np+i]=graf->vertex[i].y;
		points[2*np+i]=graf->vertex[i].z;
		}
	}
 else
	{
	//----------------------------

	points=alloc_float_mas_n1(3*(*p_np+np));

	for(i=0;i<3;i++)
	memcpy( (   points)+i*(*p_np+np),
			(*p_points)+i*(*p_np+ 0),
			(*p_np)*sizeof(float));

	free(*p_points);
	*p_points=points;

	for(i=0;i<np;i++)
		{
		points[             (i+(*p_np))]=graf->vertex[i].x;
		points[  (*p_np+np)+(i+(*p_np))]=graf->vertex[i].y;
		points[2*(*p_np+np)+(i+(*p_np))]=graf->vertex[i].z;
		}

	//----------------------------

	*p_trg=(int*)realloc(*p_trg,3*(*p_nt+graf->nt)*sizeof(int));

	if(!(*p_trg))
		{
		printf("Error realloc trg from %d to %d\n",*p_nt,*p_nt+graf->nt);
		return 1;
		}
	
	for(i=0;i<3*nt;i++)
		{
		(*p_trg)[3*(*p_nt)+i]=graf->trg[i]+(*p_np);
		}

	*p_np=*p_np+graf->n;
	*p_nt=*p_nt+graf->nt;

	free(graf->trg);
	graf->trg=NULL;

	//----------------------------
	}

//===========================================================

#if 0
/*
# plots=6 tri=5
v  0  2 0
v -1  1 0
v -1 -1 0
v  1 -1 0
v  1  1 0
v  0  0 1
f 6 2 1
f 6 3 2
f 6 4 3
f 6 5 4
f 6 5 1
*/
 np=6;
 nt=5;

#define setpoint(i,a,b,c) {  points[    i]=a;  points[ np+i]=b;  points[2*np+i]=c;}
#define settrg(i,a,b,c)   {(*p_trg)[3*i+1]=a;(*p_trg)[3*i+1]=b;(*p_trg)[3*nt+2]=c;}

 i=0;
 setpoint(i, 0, 2,0);i++;
 setpoint(i,-1, 1,0);i++;
 setpoint(i,-1,-1,0);i++;
 setpoint(i, 1,-1,0);i++;
 setpoint(i, 1, 1,0);i++;
 setpoint(i, 0, 0,1);i++;

 i=0;
 settrg(i, 5, 1, 0);i++;
 settrg(i, 5, 2, 1);i++;
 settrg(i, 5, 3, 2);i++;
 settrg(i, 5, 4, 3);i++;
 settrg(i, 5, 4, 0);i++;

 *p_np=np;
 *p_nt=nt; 

#endif

 return 0;
}
 
